package com.mitcub.panda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mitcub.panda.base.BaseQuery;
import com.mitcub.panda.base.BaseServiceImpl;
import com.mitcub.panda.entity.Dept;
import com.mitcub.panda.mapper.DeptMapper;
import com.mitcub.panda.query.DeptQuery;
import com.mitcub.panda.service.IDeptService;
import com.mitcub.panda.utils.RespResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

/**
 * @项目名称: panda
 * @类名称: DeptServiceImpl
 * @类描述: 部门 服务实现类
 * @作者: tangloong
 * @创建时间: 2021/11/2 17:22
 */
@Service
public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Resource
    private DeptMapper deptMapper;

    /**
     * 获取数据列表
     * @param query 查询条件
     * @return
     */
    @Override
    public RespResult getList(BaseQuery query) {
        DeptQuery depQuery = (DeptQuery) query;
        // 查询条件
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        // 部门名称
        if (!StringUtils.isEmpty(depQuery.getName())) {
            queryWrapper.like("name", depQuery.getName());
        }
        // 类型：1公司 2部门
        if (depQuery.getType() != null) {
            queryWrapper.eq("type", depQuery.getType());
        }
        // 是否有子级：1是 2否
        if (depQuery.getHasChild() != null) {
            queryWrapper.eq("has_child", depQuery.getHasChild());
        }

        // 类型：1公司 2部门
        if (depQuery.getPid() != null) {
            queryWrapper.eq("pid", depQuery.getPid());
        }

        queryWrapper.eq("mark", 1);
        queryWrapper.orderByAsc("sort");

        List<Dept> depList = new ArrayList<>();
        Long total  = 0L;
        if(depQuery.getType() != null && depQuery.getType() ==1){
            depList = deptMapper.selectList(queryWrapper);
            depList = getTree(depList);
        }else{
            // 查询数据
            Page<Dept> page = new Page<>(depQuery.getPage(), depQuery.getLimit());
            IPage<Dept> data = deptMapper.selectPage(page, queryWrapper);
            depList = data.getRecords();
            total = data.getTotal();
        }
        return RespResult.success("操作成功", depList, total);
    }

    private static List<Dept> getTree(List<Dept> categories){
        List<Dept> tree = new LinkedList<>();
        Map<Long,Dept> map = new HashMap<>();
        for(Dept m:categories){
            map.put(m.getId(),m);
            if(m.getPid() == 0){
                m.setChildren(new ArrayList<>());
                tree.add(m);
            }
        }
        for(Dept m:categories){
            long pid = m.getPid();
            if(pid == 0){
                continue;
            }
            Dept category = map.get(pid);
            if(null == category.getChildren()){
                category.setChildren(new ArrayList<>());
            }
            category.getChildren().add(m);
        }
        return tree;
    }


    /**
     * 根据部门ID获取部门名称
     *
     * @param depId     部门ID
     * @param delimiter 拼接字符
     * @return
     */
    @Override
    public String getDepNameByDepId(Long depId, String delimiter) {
        List<String> nameList = new ArrayList<>();
        while (depId > 0) {
            Dept depInfo = deptMapper.selectById(depId);
            if (depInfo != null) {
                nameList.add(depInfo.getName());
                depId = depInfo.getPid();
            } else {
                depId = 1L;
            }
        }
        // 使用集合工具实现数组翻转
        Collections.reverse(nameList);
        return org.apache.commons.lang3.StringUtils.join(nameList.toArray(), delimiter);
    }
}